<?php

namespace Modules\Article\Http\Controllers;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Article\Entities\Category;
use Modules\Article\Http\Requests\CategoryRequest;

class CategoryController extends Controller
{
    /**
     * @Notes:  首页功能显示
     * @Interface:          index
     * @param        Category  $category
     * @return:             Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author:             Jokenzhang
     * @Time:               2020/12/21  10:29
     */
    public function index(Category $category)
    {
        $categories = $category->getAll();
        return view('article::category.index', compact('categories'));
    }

    /**
     * @Notes:
     * @Interface:          create
     * @param        Category  $category
     * @return:             \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author:             Jokenzhang
     * @Time:               2020/12/18  8:37 下午
     */
    public function create(Category $category)
    {
        $categories = $category->getAll();
        return view('article::category.create', compact('categories'));
    }

    /**
     * @Notes:
     * @Interface:          store
     * @param        CategoryRequest  $request
     * @param        Category  $category
     * @return:             Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     * @author:             Jokenzhang
     * @Time:               2020/12/18  8:37 下午
     */
    public function store(CategoryRequest $request, Category $category)
    {
        //给模型添加属性
        $category->fill($request->all());
        $category->save();
        return redirect('/article/category')->with('success', '添加成功！');
    }

    /**
     * @Notes: 单个数据显示
     * @Interface:          show
     * @return:             Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author:             Jokenzhang
     * @Time:               2020/12/21  10:29
     */
    public function show()
    {
        return view('article::category.show');
    }

    /**
     * @Notes: 修改显示
     * @Interface:          edit
     * @param        Category  $category
     * @return:             Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author:             Jokenzhang
     * @Time:               2020/12/21  10:30
     */
    public function edit(Category $category)
    {
        $categories = $category->getAll($category);
        return view('article::category.edit', compact('category', 'categories'));
    }

    /**
     * @Notes:
     * @Interface:          update
     * @param        CategoryRequest  $request
     * @param        Category  $category
     * @return:             Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     * @author:             Jokenzhang
     * @Time:               2020/12/18  10:45 下午
     */
    public function update(CategoryRequest $request, Category $category)
    {
        $category->update($request->all());
        return redirect('/article/category')->with('success', '修改成功');
    }

    /**
     * @Notes: 删除栏目
     * @Interface:          destroy
     * @param        Category  $category
     * @return:             Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     * @throws \Exception
     * @author:             Jokenzhang
     * @Time:               2020/12/21  14:22
     */
    public function destroy(Category $category)
    {
        if ($category->hasChildCategory()) {
            return back()->with('danger', '请先删除子栏目');
        }
        $category->delete();
        return redirect('/article/category')->with('success', '栏目删除成功！');
    }
}
